package com.itggm.controller;

import cn.hutool.captcha.LineCaptcha;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


/**
 * @author: ggm
 * @data 2024年11月15日 16:11
 * @description: 验证码控制器
 * 用于生成图形验证码
 */

@RestController
@RequestMapping("/captcha")
public class CaptchaController {

    // 生成验证码
    @GetMapping
    public void createCaptcha(HttpServletRequest request, HttpServletResponse response){
        LineCaptcha lineCaptcha = new LineCaptcha(100,38,4,3);

        //获取图片验证码 并保存
        String code = lineCaptcha.getCode();
        //System.out.println("code = " + code);


        HttpSession session = request.getSession();
        session.setAttribute("code",code);
        try {
            ServletOutputStream outputStream = response.getOutputStream();
            lineCaptcha.write(outputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

}
